
var express = require('express');
var routes = require('./routes');
var user = require('./routes/user');
var applist = require('./routes/applist');
var appdetails = require('./routes/appdetails');
var sarelation = require('./routes/server_app_relation');
var http = require('http');
var path = require('path');
var engine = require('ejs-locals');
var url  = require('./utils/url.js');
var mysql = require('mysql');
var flash = require('connect-flash');
var groupHandlers = require('express-group-handlers');

var app = express();
groupHandlers.setup(app);

function createSID() {
    var pre = "SESS";
    var time = (new Date()).getTime() + '';
    var id = pre + '_' + (time).substring(time.length - 6) + '_' + (Math.round(Math.random() * 1000));
    return id;
}

app.configure(function () {
    app.set('port', process.env.PORT || url.local_port);
    app.set('views', path.join(__dirname, 'views'));
    app.engine('html',engine);
    app.set('view engine', 'html');
    app.use(express.favicon());
    app.use(express.logger('dev'));
    app.use(express.json());
    app.use(express.urlencoded());
    app.use(express.methodOverride());
    app.use(flash());
    app.use(express.static(path.join(__dirname, 'public')));
    app.use(express.cookieParser('iwrong'));
    app.use(express.session({ secret: 'keyboard cat', key: createSID(), cookie: { secure: true}, cookie: { maxAge: 60 * 60 * 1000}}));
    app.use(app.router);
});

//404
app.use(function (req, res, next) {
    res.render('404', {  status: 404, url: req.url  });
});
//500
app.use(function (err, req, res, next) {
/*    res.render('500', {
     status: err.status || 500, error: err
     });*/
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.write("<script>parent.location.href='/'</script>");
    res.end();
});

// development only
if ('development' == app.get('env')) {
  app.use(express.errorHandler());
}

app.get('/', routes.index);
app.get('/logout',routes.logout);
app.post('/login',user.login);
app.post('/validateUser',user.validateUser);
app.get('/toRegister',routes.toRegister);
app.post('/executeRegister',user.executeRegister);

app.beforeEach(function (req, res, next) {
    if (req.session.user) {
        next();
    } else {
        res.writeHead(200, {'Content-Type': 'text/html'});
        res.write("<script>parent.location.href='/'</script>");
        res.end();
    }
}, function (app) {
    app.get('/toMainPage',routes.toMainPage);
    app.get('/getServerList',routes.getServerList);

    app.post('/createServer',routes.createServer);
    app.post('/editServer',routes.editServer);
    app.get('/deleteServer',routes.deleteServer);

    app.get('/getAllApp',applist.getAllApp);
    app.post('/createAppInfo',applist.createAppInfo);
    app.post('/editAppInfo',applist.editAppInfo);
    app.get('/deleteApp',applist.deleteApp);

    app.get('/getAppDetails',appdetails.getAppDetails);
    app.post('/createAppDetails',appdetails.createAppDetails);
    app.post('/editAppDetails',appdetails.editAppDetails);
    app.get('/deleteAppDetails',appdetails.deleteAppDetails);

    app.get('/toAppList' ,sarelation.toAppList);
    app.get('/getAppName' ,sarelation.getAppName);
    app.post('/addServer_App',sarelation.addServer_App);
    app.post('/hasAppId',sarelation.hasAppId);
    app.get('/deleteS_app',sarelation.deleteS_app);

    app.get('/toPlatform',sarelation.toPlatform);
    app.get('/getPlatformName',sarelation.getPlatformName);
    app.post('/addPlatformName',sarelation.addPlatformName);
    app.post('/hasPlatformId',sarelation.hasPlatformId);
    app.get('/deletePlatform',sarelation.deletePlatform);
});

http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});


